package com.unicom.mec.project.service;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.unicom.mec.common.Exception.BusinessException;
import com.unicom.mec.project.mapper.ProjectMapper;
import com.unicom.mec.project.model.Project;
import com.unicom.mec.project.vo.AddProjectVo;
import com.unicom.mec.project.vo.EditProjectVo;
import com.unicom.mec.project.vo.ProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 项目业务层实现
 *
 * @author 刘亮武
 * @date 2020年06月12日
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;


    @Override
    public Long addProject(AddProjectVo addProjectVo) {
        Long id = IdWorker.getId();
        Project project = new Project();
        project.setId(id);
        project.setTenantId(addProjectVo.getTenantId());
        project.setProjectName(addProjectVo.getProjectName());
        project.setProjectDesc(addProjectVo.getProjectDesc());
        project.setLongitude(addProjectVo.getLongitude());
        project.setLatitude(addProjectVo.getLatitude());
        project.setCreateId(1271322350203121664L);
        project.setCreateTime(new Date());
        projectMapper.insert(project);
        return id;
    }

    @Override
    public ProjectVo editProject(EditProjectVo editProjectVo) {
        Project project = projectMapper.selectById(editProjectVo.getId());
        if(project == null){
            throw new BusinessException("项目不存在");
        }
        project.setProjectName(editProjectVo.getProjectName());
        project.setProjectDesc(editProjectVo.getProjectDesc());
        project.setLongitude(editProjectVo.getLongitude());
        project.setLatitude(editProjectVo.getLatitude());
        project.setUpdateId(1271322350203121664L);
        project.setUpdateTime(new Date());
        projectMapper.updateById(project);
        ProjectVo projectVo = projectMapper.findProjectById(editProjectVo.getId());
        return projectVo;
    }

    @Override
    public ProjectVo findProjectById(Long id) {
        return projectMapper.findProjectById(id);
    }

    @Override
    public void deleteProject(Long id) {
        projectMapper.deleteById(id);
    }

    @Override
    public List<ProjectVo> findProjects(String projectName, Long tenantId) {
        return projectMapper.findProjects(projectName, tenantId);
    }
}
